package com.study;

import cn.hutool.core.io.FileUtil;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.QueryApi;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**8
 from(bucket: "test_java")
 |> range(start: -1h)
 |> filter(fn: (r) => r._measurement == "wave" and r._field == "rawdata")
 |> yield(name: "str_data")


 from(bucket: "test_java")
 |> range(start: -1h)
 |> filter(fn: (r) => r._measurement == "wave" and r._field == "rawdata")
 |> map(fn: (r) => ({
 _measurement: r._measurement,
 _time: r._time,
 rawdata: r._value,
 _field: r._field
 }))
 |> yield(name: "str_data")


 from(bucket: "smqtt")
 |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
 |> filter(fn: (r) => r["_measurement"] == "centreIotData")
 |> map(fn: (r) => ({
 _measurement: r._measurement,
 _time: r._time,
 rawdata: r._value,
 _field: r._field
 }))
 |> yield(name: "mean")


 from(bucket: "smqtt")
 |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
 |> filter(fn: (r) => r["_measurement"] == "centreIotData")
 |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
 |> yield(name: "mean")

 */
public class A {
    private static char[] token = "oLpHiTew0eh9JtzdbmQLXFxKC4cuJvpfzn_NOiqR4fzhYWLmOci5c99YT5_6tILmYDTAYbDF4F7Vp1vl6jVJKw==".toCharArray();

    private static String bucket = "test_java";
    public static void main(String[] args) throws IOException {
        // 连接到 InfluxDB
        InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://192.168.112.129:8086", token,"xyyl", bucket);

        // 创建查询对象
        QueryApi queryApi = influxDBClient.getQueryApi();
        //拼接Flux查询语句
        String query = " from(bucket: \"smqtt\")\n" +
                " |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n" +
                " |> filter(fn: (r) => r[\"_measurement\"] == \"centreIotData\")\n" +
                " |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")\n" +
                " |> yield(name: \"mean\")";

        // 执行查询
        List<FluxTable> queryTableResult = queryApi.query(query, "xyyl");


    }

}
